import moment from 'moment'

/* eslint-disable */
let tool = {

  // 深拷贝
  deepCopy (obj) {
    // if (!obj && typeof (obj) !== 'undefined' && obj !== 0) return // null
    if (typeof obj !== 'object') {
      return obj
    } else {
      let _obj = (obj instanceof Array ? [] : {})
      for (let prop in obj) {
        _obj[prop] = this.deepCopy(obj[prop])
      }
      return _obj
    }
  },
  // 格式化 Date对象：日期对象->字符串 YYYY-MM-DD
  formatDate (date) {
    let _month = String(date.getMonth() + 1)
    let _date = String(date.getDate())
    if (_month.length === 1) _month = '0' + _month
    if (_date.length === 1) _date = '0' + _date
    return date.getFullYear() + '-' + _month + '-' + _date
  },
  dateFormat (utc, onlyDate, isHistory) {
    let date = moment(utc).utcOffset('8')
    let now = moment()
    if (isHistory) {
      return utc.substr(0, 5)
    } else {
      return now.isSame(date._i, 'day')
        ? '今天 ' + date.format('HH:mm')
        : now.isSame(date._i, 'year')
          ? onlyDate ? date.format('MM-DD') : date.format('MM-DD HH:mm')
          : onlyDate ? date.format('YYYY-MM-DD') : date.format('YYYY-MM-DD HH:mm')
    }
  }
}

export default tool
